package com.interview.net;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpRecv {
    public static void main(String[] args) {
        DatagramPacket dp = null;
        DatagramSocket ds = null;
        byte[] buf = new byte[1024];
        try {
            ds = new DatagramSocket(3000);
            dp = new DatagramPacket(buf, 1024);
            //接受数据
            System.out.println("客户端启动成功，准备接收数据:");
            ds.receive(dp);
            String string = new String(buf, 0, buf.length);
            string = string + " from " + dp.getAddress().getHostAddress();
            System.out.println(string);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ds.close();
        }
    }

}
